package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.ISystemMapper;
import com.cuixichang.minimalism.transfer.dto.ISystemDTO;
import com.cuixichang.minimalism.transfer.factory.ISystemAssembler;
import com.cuixichang.minimalism.transfer.po.ISystem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 系统信息注册表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class ISystemServiceImpl implements ISystemService {

    @Autowired
    private ISystemMapper iSystemMapper;

    @Override
    public ISystemDTO selectById(Long id){
        ISystem iSystem = iSystemMapper.selectById(id);
        return ISystemAssembler.INSTANCT.convertDto(iSystem);
    }

    @Override
    public ISystemDTO selectOne(ISystemDTO iSystemDTO){
        Assert.notNull(iSystemDTO,"参数为空,请检查");
        ISystem iSystem = ISystemAssembler.INSTANCT.convertPo(iSystemDTO);
        ISystem result = iSystemMapper.selectOne(new QueryWrapper<>(iSystem));
        return ISystemAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<ISystemDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<ISystem> results = iSystemMapper.selectBatchIds(ids);
        return results.stream().map(ISystemAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<ISystemDTO> selectList(ISystemDTO iSystemDTO){
        return this.selectList(iSystemDTO, (iSystem, queryWrapper) -> {});
    }

    @Override
    public List<ISystemDTO> selectList(ISystemDTO iSystemDTO, BiConsumer<ISystem, QueryWrapper<ISystem>> consumer){
        Assert.notNull(iSystemDTO,"参数为空,请检查");
        ISystem iSystem = ISystemAssembler.INSTANCT.convertPo(iSystemDTO);
        QueryWrapper<ISystem> queryWrapper = new QueryWrapper<>(iSystem);
        if(Objects.nonNull(consumer)){
            consumer.accept(iSystem, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<ISystem> results = iSystemMapper.selectList(queryWrapper);
        return results.stream().map(ISystemAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<ISystemDTO> selectPage(ISystemDTO iSystemDTO,long pageSize,long pageNo){
        return this.selectPage(iSystemDTO, pageSize, pageNo, (iSystem, queryWrapper) -> {});
    }

    @Override
    public Page<ISystemDTO> selectPage(ISystemDTO iSystemDTO,long pageSize,long pageNo, BiConsumer<ISystem, QueryWrapper<ISystem>> consumer){
        Assert.notNull(iSystemDTO,"参数为空,请检查");
        ISystem iSystem = ISystemAssembler.INSTANCT.convertPo(iSystemDTO);
        QueryWrapper<ISystem> queryWrapper = new QueryWrapper<>(iSystem);
        if(Objects.nonNull(consumer)) consumer.accept(iSystem, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<ISystem> page = iSystemMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<ISystemDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(ISystemAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iSystemMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iSystemMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(ISystemDTO iSystemDTO){
        return deleteByCondition(iSystemDTO, (iSystem, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(ISystemDTO iSystemDTO, BiConsumer<ISystem, QueryWrapper<ISystem>> consumer){
        Assert.notNull(iSystemDTO,"参数为空,请检查");
        ISystem iSystem = ISystemAssembler.INSTANCT.convertPo(iSystemDTO);
        QueryWrapper<ISystem> queryWrapper = new QueryWrapper<>(iSystem);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iSystem, queryWrapper);
        }
        return iSystemMapper.delete(queryWrapper);
    }

    @Override
    public ISystemDTO insert(ISystemDTO iSystemDTO){
        return insertHasOption(iSystemDTO, (ISystemDTO) -> null);
    }

    @Override
    public ISystemDTO insertHasOption(ISystemDTO iSystemDTO, Function<ISystemDTO,ISystemDTO> preOption){
        Assert.notNull(iSystemDTO,"参数为空,请检查");
        iSystemDTO.check();
        ISystem iSystem = ISystemAssembler.INSTANCT.convertPo(iSystemDTO);
        Long createLoginId = iSystem.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iSystem.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iSystem.setCreateTime(localDateTime);
        iSystem.setUpdateTime(localDateTime);
        Boolean isEnable = iSystem.getIsEnable();
        if(Objects.isNull(isEnable)) iSystem.setIsEnable(true);
        Integer sort = iSystem.getSort();
        if(Objects.isNull(sort)) iSystem.setSort(0);
        if(Objects.nonNull(preOption)){
            ISystemDTO apply = preOption.apply(iSystemDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iSystemMapper.insert(iSystem);
        Assert.isTrue(insert>0,"插入失败");
        return ISystemAssembler.INSTANCT.convertDto(iSystem);
    }

    @Override
    public int updateById(ISystemDTO iSystemDTO){
         return updateById(iSystemDTO, (iSystem, updateWrapper) -> {});
    }

    @Override
    public int updateById(ISystemDTO iSystemDTO, BiConsumer<ISystem, UpdateWrapper<ISystem>> consumer){
        Assert.notNull(iSystemDTO,"参数为空,请检查");
        Long id = iSystemDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        ISystem iSystem = ISystemAssembler.INSTANCT.convertPo(iSystemDTO);
        Long createLoginId = iSystem.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iSystem.setCreateLoginId(null);
        iSystem.setUpdateLoginId(createLoginId);
        iSystem.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<ISystem> updateWrapper = new UpdateWrapper<ISystem>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iSystem,updateWrapper);
        return iSystemMapper.update(iSystem,updateWrapper);
    }
}
